package com.atwisdom.star.common.util;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.googlecode.aviator.AviatorEvaluator;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 公式工具
 */
public class ExpressionUtil {


    /**
     * 处理公式数据
     * 更新公式 ${arrivalQuantity} = ${arrivalQuantity} + ${current.quantity}
     * @param expression
     * @return
     *      arrivalQuantity
     *      arrivalQuantity
     *      current.quantity
     */
    public static String[] handleExpression(String expression) {
        String[] arr = StringUtils.split(expression, "${");
        String[] result = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            String value = arr[i].split("}")[0];
            result[i] = value;
        }
        return result;
    }


    public static String fetchByReg(String expression, String reg) {
        // 处理sum公式
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(expression);
        if (matcher.find()) {
            return matcher.group(1);
        } else {
            return null;
        }
    }

    public static Object handleExpression(JSONObject source, String sourceKey) {
        String originSourceKey = sourceKey;
        String[] expressionArray =  ExpressionUtil.handleExpression(sourceKey);
        Object tempValue;
        for (int i = 0; i < expressionArray.length; i++) {
            String expression = expressionArray[i];
            tempValue = JsonUtil.get(source, expression);
            tempValue = tempValue == null ? 0:tempValue;
            sourceKey = sourceKey.replace(expressionArray[i], String.valueOf(tempValue));
            sourceKey = sourceKey.replace("${","");
            sourceKey = sourceKey.replace("}","");
        }
        if (originSourceKey.contains("${")) {
            return AviatorEvaluator.execute(sourceKey);
        } else {
            return sourceKey;
        }
    }

    /**
     * 用map中的内容，替换originStr中的内容
     * @param originStr
     * @param dataMap
     * @return
     */
    public static String replaceInnerBrace(String originStr, Map<String, String> dataMap) {
        Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");
        Matcher matcher = pattern.matcher(originStr);
        List<String> matches = new ArrayList<>();
        while (matcher.find()) {
            matches.add(matcher.group(1));
        }
        // 存在一个语句中有多个$的情况
        if (CollectionUtil.isNotEmpty(matches)) {
            for (String match : matches) {
                String findResult = dataMap.get(match);
                originStr = originStr.replace("${" + match + "}", String.valueOf(findResult));
            }
        }
        return originStr;
    }

}